/**
* File Name: pthrd_detach1.c
* Auther: Jichang
* Environment: Fedora 7 server
* test pthread_detach function.
*/
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<pthread.h>

void *tfn(void *arg)
{
	printf("thread: pid=%d, tid=%lu\n", getpid(), pthread_self());
	return NULL;
}
int main(int argc, char *argv[])
{
	pthread_t tid;

	int ret = pthread_create(&tid, NULL, tfn, NULL);
	if(ret != 0) {
		fprintf(stderr, "pthread_create error: %s\n", strerror(ret));
	}

	/* set thread depart, reclaim auto */
	/* it will clean PCB, don't need to reclaim */
	ret = pthread_detach(tid);
	if(ret != 0) {
		fprintf(stderr, "pthread_detach error: %s\n", strerror(ret));
	}
	sleep(1);
	
	// Invalid argument because of detach.
	ret = pthread_join(tid, NULL);
	printf("join ret = %d\n", ret);
	if(ret != 0) {
		fprintf(stderr, "pthread_join error: %s\n", strerror(ret));
	}

	printf("main: pid=%d, tid=%lu\n", getpid(), pthread_self());
	pthread_exit((void*)0);
	
}

